#include <stdio.h>
#include "Private.h"
#include "Network.h"
#include <linux/types.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <System.h>
#include <string.h>

/// <summary>
/// TODO
/// </summary>
int GetAdapter(void)
{
	/*
	int netlink = -1;

	if ((netlink = NetLinkOpen()) < 0)
		return -1;

	printf("Netlink: %d\n", netlink);*/

	struct ifreq *ifr, *ifend;
    struct ifreq ifreq;
    struct ifconf ifc;
    struct ifreq ifs[64];
    int SockFD;

    SockFD = CreateSocket(AF_INET, SOCK_DGRAM, 0);

    ifc.ifc_len = sizeof(ifs);
    ifc.ifc_req = ifs;
    if (IoControl(SockFD, SIOCGIFCONF, &ifc) < 0)
    {
        printf("ioctl(SIOCGIFCONF): %m\n");
        return 0;
    }


    ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
    for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
    {
	    printf("Device %x\n", ifr->ifr_addr.sa_family);
	    
        if (ifr->ifr_addr.sa_family == AF_INET)
        {
            strncpy(ifreq.ifr_name, ifr->ifr_name,sizeof(ifreq.ifr_name));
            if (IoControl (SockFD, SIOCGIFHWADDR, &ifreq) < 0)
            {
              printf("SIOCGIFHWADDR(%s): %m\n", ifreq.ifr_name);
              return 0;
            }

//printf("Ip Address %s\n", inet_ntoa( ( (struct sockaddr_in *)  &ifr->ifr_addr)->sin_addr)); 
      printf("Device %s -> Ethernet %02x:%02x:%02x:%02x:%02x:%02x\n", ifreq.ifr_name,
      (int) ((unsigned char *) &ifreq.ifr_hwaddr.sa_data)[0],
      (int) ((unsigned char *) &ifreq.ifr_hwaddr.sa_data)[1],
      (int) ((unsigned char *) &ifreq.ifr_hwaddr.sa_data)[2],
      (int) ((unsigned char *) &ifreq.ifr_hwaddr.sa_data)[3],
      (int) ((unsigned char *) &ifreq.ifr_hwaddr.sa_data)[4],
      (int) ((unsigned char *) &ifreq.ifr_hwaddr.sa_data)[5]);
 }
 }
	return 0;
}